<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework										|
 * | Version 1.0																|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>					|
 * | Blog: http://mcezzare.blogspot.com.br										|
 * |																			|
 * | This program is part of PHPKiisf, and is free software: you can			|
 * | redistribute it and/or modify it under the terms of the GNU 				|
 * | General Public License as published by										|
 * | the Free Software Foundation, either version 3 of the License, or			|
 * | (at your option) any later version.										|
 * |																			|
 * | This program is distributed in the hope that it will be useful,			|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of				|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				|
 * | GNU General Public License for more details.								|
 * | You should have received a copy of the GNU General Public License			|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.		|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>			|
 * +----------------------------------------------------------------------------+
 * | Contributors																|
 * |																			|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * @package Lib
 * @access  public
 * @since   2013-03-08
 * @author Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * Carrega as classes necessárias quando instanciadas.
 */
class autoloader {

		/**
		 * Função que inicia o autoloader
		 *
		 * @global type $debug
		 * @param string $classe Nome da classe, a função adiciona o .class.php
		 */
		public static function loader( $classe ) {
				global $debug;
				/**
				 *  Para sair no aviso da linhas com debug ligado
				 */
				$PREFIX_DEBUG_MESSAGE = "\n[Autoloader] - ";

				// como chega o nome ao instanciar
				$debug == true ? display_user_message( sprintf( "%s receiving <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , $classe ) ) : null;
				/**
				 * Quero apenas o que tem apos a utima barra -- ex app\model\Foo
				 * Isso apenas em caso de utiizar namespaces
				 * Retorna apenas o nome da classe
				 */
				$nameClassOnly	 = strpos( strrev( $classe ) , "\\" );
				$classeAux		 = substr( $classe , -$nameClassOnly );

//		if ($debug == true) {
//			echo "..would be <b>$classeAux</b>??<br>";
//		}
				// Acerta as barras p/ funcionar com namespaces
				$finalClass		 = str_replace( '\\' , '/' , $classe );
				$class_name_file = $finalClass . '.class.php';

				// Verifica se o arquivo existe antes de tentar incluir
				try {
						// se for lib
						if ( file_exists( LIB_DIR . $class_name_file ) ) {

								$debug == true ? display_user_message( sprintf( "%s...including <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , LIB_DIR . $class_name_file ) ) : null;
								require_once LIB_DIR . $class_name_file;

								// Registra as classes passando o método statico init
								spl_autoload_register( array( $classe , 'init' ) );
								return;
						}

						// se for controller
						if ( file_exists( CONTROLLER_DIR . $class_name_file ) ) {
								$debug == true ? display_user_message( sprintf( "%s...including <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , CONTROLLER_DIR . $class_name_file ) ) : null;
								require_once CONTROLLER_DIR . $class_name_file;

								// Registra as classes passando o método statico init
								spl_autoload_register( array( $classe , 'init' ) );
								return;
						}

						// se for driver de banco
						if ( file_exists( LIB_DB_DRIVERS . $class_name_file ) ) {
								$debug == true ? display_user_message( sprintf( "%s...including <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , LIB_DB_DRIVERS . $class_name_file ) ) : null;
								require_once LIB_DB_DRIVERS . $class_name_file;

								// Registra as classes passando o método statico init
								spl_autoload_register( array( $classe , 'init' ) );
								return;
						}
						// se for classe de dao
						if ( file_exists( DAO_DIR . $class_name_file ) ) {
								$debug == true ? display_user_message( sprintf( "%s...including <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , LIB_DB_DRIVERS . $class_name_file ) ) : null;
								require_once DAO_DIR . $class_name_file;

								// Registra as classes passando o método statico init
								spl_autoload_register( array( $classe , 'init' ) );
								return;
						}
						// se for classe model
						if ( file_exists( MODELS_DIR . $class_name_file ) ) {
								$debug == true ? display_user_message( sprintf( "%s...including <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , LIB_DB_DRIVERS . $class_name_file ) ) : null;
								require_once MODELS_DIR . $class_name_file;

								// Registra as classes passando o método statico init
								spl_autoload_register( array( $classe , 'init' ) );
								return;
						}

						// Alguns templates foram organizados em classes, logo
						if ( file_exists( TEMPLATES_DIR . $class_name_file ) ) {
								$debug == true ? display_user_message( sprintf( "%s...including <b>%s</b>" , $PREFIX_DEBUG_MESSAGE , TEMPLATES_DIR . $class_name_file ) ) : null;
								require_once TEMPLATES_DIR . $class_name_file;

								// Registra as classes passando o método statico init
								spl_autoload_register( array( $classe , 'init' ) );
								return;
						}


//			else {
						throw new Exception( "$PREFIX_DEBUG_MESSAGE CLASS_NOT_FOUND:$class_name_file" , 13 );
						return false;
//			}
				}
				catch ( Exception $E ) {
						echo $E->getMessage();
				}
		}

		/**
		 *
		 * @param type $file
		 *
		 */
		public static function include_file( $file ) {

		}

}

/**
 * Nao precisa usar ---- $autoloader = new Autoloader();
 * Pois a função loader acima já cria uma instância dessa classe
 * pela chamada spl_autoload_register('autoloader::loader');
 * O Frameword pede um método init p/ registro automático
 */
spl_autoload_register( 'autoloader::loader' );
?>